package tool;

import CommonStructure.ListNode;
import CommonStructure.TreeNode;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Map;
import java.util.Scanner;

public class s {
    public static void main(String[] args) {
        TreeNode treeNode = setTreeNodeByArray(new int[]{1, 2, 3, -1, -1, 4, 5});
        printTreeNode(treeNode);
    }

    public static void print(int[] nums) {
        String str = "[";
        for (int i = 0; i < nums.length; i++) {
            str += nums[i];
            if (i == nums.length - 1) {
                str += "]";
            } else {
                str += ",";
            }
        }
        System.out.println(str);
    }

    public static void print2(int[][] nums) {
        for (int i = 0; i < nums.length; i++) {
            StringBuilder str = new StringBuilder("[");
            for (int j = 0; j < nums[i].length; j++) {
                str.append(nums[i][j]);
                if (j == nums[i].length - 1) {
                    str.append("]");
                } else {
                    str.append(",");
                }
            }
            System.out.println(str);
        }
    }

    public static int[] ScIntArray(int n) {
        int[] nums = new int[n];
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < n; i++) {
            if (scanner.hasNextInt()) {
                nums[i] = scanner.nextInt();
            }
        }
        return nums;
    }


    public static void print(Object[] nums) {
        String str = "[";
        for (int i = 0; i < nums.length; i++) {
            str += nums[i];
            if (i == nums.length - 1) {
                str += "]";
            } else {
                str += ",";
            }
        }
        System.out.println(str);
    }

    public static ListNode setListNodeByArray(int[] nums) {
        ListNode head = new ListNode();
        ListNode pre = head;
        for (int i = 0; i < nums.length; i++) {
            pre.next = new ListNode(nums[i]);
            pre = pre.next;
        }
        return head.next;
    }

    public static TreeNode setTreeNodeByArray(int[] nums) {
        Deque<TreeNode> deque = new ArrayDeque<>();
        TreeNode root = new TreeNode(nums[0]);
        deque.push(root);
        for (int i = 1; i < nums.length; ) {
            int size = deque.size();
            for (int j = 0; j < size; j++) {
                TreeNode l, r, f = deque.pop();
                l = new TreeNode(nums[i]);
                r = new TreeNode(nums[i + 1]);
                i += 2;
                f.left = l;
                f.right = r;
                deque.push(l);
                deque.push(r);
            }
        }
        return root;
    }

    public static void printTreeNode(TreeNode root) {
        if (root != null) {
            if (root.left != null) {
                System.out.println(root.val + "->" + root.left.val);
            } else {
                System.out.println(root.val + "->null");
            }
            if (root.right != null) {
                System.out.println(root.val + "->" + root.right.val);
            } else {
                System.out.println(root.val + "->null");
            }
            printTreeNode(root.left);
            printTreeNode(root.right);
        }
    }

    public static void printMap(Map<Integer, Integer> map) {
        System.out.println("printMap:");
        for (Map.Entry entry : map.entrySet()) {
            System.out.println("       "+entry.getKey() + "--" + entry.getValue());
        }
    }


}
